package org.hot100_3;

import java.util.HashMap;
import java.util.Stack;

public class leetcode20 {
    public boolean isValid(String s) {

        HashMap<Character,Character> hashMap = new HashMap<>();

        if (s.length() % 2 != 0) {
            return false;
        }

        hashMap.put('(',')');
        hashMap.put('{','}');
        hashMap.put('[',']');

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))){
                stack.push(hashMap.get(s.charAt(i)));
            }else {
                if (stack.isEmpty() || stack.peek()!=s.charAt(i)){
                    return false;
                }else {
                    stack.pop();
                }
            }
        }

        return stack.isEmpty();
    }
}
